package message.interceptor;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.interceptor.Interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Interceptor
public class LoginInterceptor extends HandlerInterceptorAdapter{
	private String toURL; //������Ҫ����������غ���ת��ҳ��
	private List<String> excludedUrls;  //excludedUrls��Ҫ���е�url
	public String getToURL() {
		return toURL;
	}

	public void setToURL(String toURL) {
		this.toURL = toURL;
	}
	public List<String> getExcludedUrls() {
		return excludedUrls;
	}

	public void setExcludedUrls(List<String> excludedUrls) {
		this.excludedUrls = excludedUrls;
	}
	// ��д preHandle()��������ҵ��������������֮ǰ�Ը�����������ش���
	 @Override
	    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
	            Object handle) throws Exception {
	        
	        String requestUri = request.getRequestURI();
	        for (String url : excludedUrls) {
	            if (requestUri.endsWith(url)) {
	                return true;
	            }
	        }
	        HttpSession session = request.getSession();
	        if (session.getAttribute("host") == null) {
	            response.sendRedirect(request.getContextPath() + "/user/login");
	            ModelAndView mav=new ModelAndView();
	            mav.addObject("login_fail", "���ȵ�½");
	            return false;
	        }else{
			return true;
	        }
	    }
	
	@Override
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
					throws Exception {
		// TODO Auto-generated method stub
	}
	 @Override
	    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
	            Object arg2, ModelAndView arg3) throws Exception {
	        
	    }


}
